import React, { Component, Fragment } from 'react';
import { connect } from 'dva'
import BaseTable from './baseTable'
import { Space, Popconfirm, Radio, Input, Modal, Button, Popover, Spin, Table } from 'antd'
import constant from '../../constant/constant'
import utils from '../../utils/utils'

class PatentDataTable extends BaseTable {
    constructor(props) {
        super(props),
            this.state = {
                pageSize: 10,
                current: 1,
                column: [
                    { title: "序号", dataIndex: "id", key: "id", width: 80, fixed: 'left', },
                    { title: "标题", dataIndex: "patentTitle", key: "patentTitle", width: 320, ...this.getColumnSearchProps('patentTitle') },
                    {
                        title: "摘要", dataIndex: "abstract", key: "abstract", width: 100,
                        render: (text, record, index) => {
                            return (
                                <a onClick={() => { this.abstractText(record.abstract) }}>查看详情</a>
                            )
                        }
                    },
                    { title: "文献页数", dataIndex: "patentPages", key: "patentPages", width: 90 },
                    { title: "公开号", dataIndex: "publicationNumber", key: "publicationNumber", width: 100, ...this.getColumnSearchProps('publicationNumber') },
                    {
                        title: "公开日", dataIndex: "publlcationDate", key: "publlcationDate", width: 130, ...this.getColumnSearchProps('publlcationDate'),
                        render: (text, record, index) => {
                            if (record.publlcationDate) {
                                return (
                                    record.publlcationDate.substring(0, 10)
                                )
                            }
                        }
                    },
                    { title: "申请号", dataIndex: "applicationNumber", key: "applicationNumber", width: 100, ...this.getColumnSearchProps('applicationNumber') },
                    {
                        title: "申请日", dataIndex: "applicationDate", key: "applicationDate", width: 130, ...this.getColumnSearchProps('applicationDate'),
                        render: (text, record, index) => {
                            if (record.applicationDate) {
                                return (
                                    record.applicationDate.substring(0, 10)
                                )
                            }
                        }
                    },
                    { title: "申请人", dataIndex: "applicant", key: "applicant", width: 200, ...this.getColumnSearchProps('applicant') },
                    { title: "申请人省市（代码）", dataIndex: "applicantProvince", key: "applicantProvince", width: 180, ...this.getColumnSearchProps('applicantProvince') },
                    {
                        title: "申请人类型", dataIndex: "applicantType", key: "applicantType", width: 130,
                        filters: [
                            { text: "个体", value: "个体" },
                            { text: "企业", value: "企业" },
                            { text: "高校", value: "高校" },
                            { text: "科研单位", value: "科研单位" },
                            { text: "产学合作", value: "产学合作" },
                            { text: "大专院校", value: "大专院校" },
                            { text: "其他", value: "其他" }
                        ],
                        onFilter: (value, record) => record.applicantType.indexOf(value) === 0
                    },
                    { title: "权利要求数量", dataIndex: "claimNumber", key: "claimNumber", width: 120 },
                    {
                        title: "首次公开日", dataIndex: "firstOpenDay", key: "firstOpenDay", width: 130,
                        render: (text, record, index) => {
                            if (record.firstOpenDay) {
                                return (
                                    record.firstOpenDay.substring(0, 10)
                                )
                            }
                        }
                    },
                    {
                        title: "授权公告日", dataIndex: "privilegedTime", key: "privilegedTime", width: 130,
                        render: (text, record, index) => {
                            if (record.privilegedTime) {
                                return (
                                    record.privilegedTime.substring(0, 10)
                                )
                            }
                        }
                    },
                    {
                        title: "专利类型", dataIndex: "patentType", key: "patentType", width: 160,
                        filters: [
                            { text: "发明授权", value: "发明授权" },
                            { text: "实用新型", value: "实用新型" },
                            { text: "外观设计", value: "外观设计" },
                        ],
                        onFilter: (value, record) => record.patentType.indexOf(value) === 0
                    },
                    { title: "发明人", dataIndex: "inventor", key: "inventor", width: 160, ...this.getColumnSearchProps('inventor') },
                    { title: "发明人数量", dataIndex: "inventorNumber", key: "inventorNumber", width: 110 },
                    { title: "主分类号", dataIndex: "mainClassification", key: "mainClassification", width: 160, ...this.getColumnSearchProps('mainClassification') },
                    { title: "IPC", dataIndex: "ipc", key: "ipc", width: 130, ...this.getColumnSearchProps('ipc') },
                    { title: "IPC分类数量", dataIndex: "ipcNumber", key: "ipcNumber", width: 110 },
                    { title: "简单同族个数", dataIndex: "patentFamily", key: "patentFamily", width: 120 },
                    { title: "扩展同族个数", dataIndex: "extendedFamily", key: "extendedFamily", width: 120 },
                    { title: "inpadoc同族个数", dataIndex: "inpadocFamily ", key: "inpadocFamily", width: 140 },
                    { title: "引证专利", dataIndex: "patentCitation", key: "patentCitation ", width: 180 },
                    { title: "引证专利数量", dataIndex: "patentCitationNumber", key: "patentCitationNumber", width: 120 },
                    { title: "被引证次数", dataIndex: "forwardCitationNumber", key: "forwardCitationNumber", width: 110 },
                    { title: "引证科技文献", dataIndex: "literatureCitation", key: "literatureCitation", width: 300 },
                    { title: "引证科技文献数", dataIndex: "literatureCitationNumber", key: "literatureCitationNumber", width: 130 },
                    { title: "专利寿命（月）", dataIndex: "patentLife", key: "patentLife", width: 140 },
                    { title: "转让执行日", dataIndex: "transferDate", key: "transferDate", width: 110 },
                    { title: "被引证国别(forward)", dataIndex: "forwardCitationCountry", key: "forwardCitationCountry", width: 170 },
                    { title: "公开国别", dataIndex: "publlcationCountry", key: "publlcationCountry", width: 120, ...this.getColumnSearchProps('publlcationCountry') },
                ],
                abstractVisiable: false,
                abstractText: "",
                deleteId: [],
            }
    }

    abstractText = text => {
        this.setState({
            abstractVisiable: true,
            abstractText: text
        })
    }

    showOffModal = () => {
        this.setState({
            abstractVisiable: false
        })
    }

    changeLoading = loading => {
        if (loading == true) {
            this.setState({
                loading: <Spin></Spin>
            })
        }

        if (loading == false) {
            this.setState({
                loading: false
            })
        }
    }

    componentDidMount() {
        //加载此表格前先清空上次选择的数据并初始化页码条数及查询条件
        this.props.dispatch({
            type: "patentData/setPagination",
            data: {
                pageSize: 10,
                current: 1
            }
        })

        this.props.dispatch({
            type: "patentData/setQueryParam",
            data: {}
        })

        this.props.dispatch({
            type: "patentData/setAbstractList",
            data: []
        })

        this.props.dispatch({
            type: "patentData/setSelectedIds",
            data: []
        })

        this.props.dispatch({
            type: "patentData/setSelectedRows",
            data: []
        })

        this.props.dispatch({
            type: "patentData/getPatentDataTableList"
        })
    }

    onChangeTableRow = (record, selected) => {
        if (selected == true) {
            let _selectedIds = JSON.parse(JSON.stringify(this.props.selectedIds));
            let _selectedRows = JSON.parse(JSON.stringify(this.props.selectedRows));
            _selectedIds.push(record.id)
            _selectedRows.push(record)

            this.props.dispatch({
                type: "patentData/setSelectedIds",
                data: _selectedIds
            })

            this.props.dispatch({
                type: "patentData/setSelectedRows",
                data: _selectedRows
            })
        }

        if (selected == false) {
            let _selectedIds = JSON.parse(JSON.stringify(this.props.selectedIds));
            let _selectedRows = JSON.parse(JSON.stringify(this.props.selectedRows));
            for (let i = 0; i < _selectedIds.length; i++) {
                if (_selectedIds[i] == record.id) {
                    _selectedIds.splice(i, 1)
                }
            }

            for (let i = 0; i < _selectedRows.length; i++) {
                if (_selectedRows[i].id == record.id) {
                    _selectedRows.splice(i, 1)
                }
            }

            this.props.dispatch({
                type: "patentData/setSelectedIds",
                data: _selectedIds
            })

            this.props.dispatch({
                type: "patentData/setSelectedRows",
                data: _selectedRows
            })
        }
    }

    onChange = (selected, selectedRows, changeRows) => {
        let _selectedRows = []
        for (let i = 0; i < selectedRows.length; i++) {
            for (let key in selectedRows[i]) {
                if (key == 'abstract') {
                    _selectedRows.push(selectedRows[i][key])
                }
            }
        }
        this.props.dispatch({
            type: "patentData/setAbstractList",
            data: _selectedRows
        })

        this.props.dispatch({
            type: "patentData/setSelectedIds",
            data: selected
        })

        this.props.dispatch({
            type: "patentData/setSelectedRows",
            data: selectedRows
        })
    }

    render() {
        const rowSelection = {
            selections: [
                Table.SELECTION_ALL,
                Table.SELECTION_NONE,
            ],
            onChange: this.onChange,
            renderCell: this.renderCell,
            getCheckboxProps: this.getCheckboxProps,
            columnWidth: 40
        }
        return (
            <Fragment>
                <Modal
                    title={"专利数据摘要"}
                    onOk={this.showOffModal}
                    onCancel={this.showOffModal}
                    visible={this.state.abstractVisiable}
                >
                    {this.state.abstractText}
                </Modal>
                {super.render(this.state.column, this.props.dataSource, {
                    scroll: { x: 1080, y:580 },
                    rowKey: record => record.id,
                    rowSelection: rowSelection,
                    width: 4600,
                    pageSize: this.props.pageSize,
                    current: this.props.current,
                    loading: this.props.patentDataTableLoading ? <Spin></Spin> : false,
                    onChange: (page, pageSize) => {
                        this.props.dispatch({
                            type: "patentData/setPagination",
                            data: {
                                pageSize: pageSize,
                                current: page
                            }
                        })

                        if (utils.isEmptyObject(this.props.queryParam)) {
                            this.props.dispatch({
                                type: "patentData/getPatentDataTableList"
                            })
                        } else {
                            this.props.dispatch({
                                type: "patentData/getPatentDataSearch"
                            })
                        }

                    },
                    total: this.props.total,
                    pageSizeOptions: [10, 20, 50, 100, 200, 500, this.props.total],
                    isPagination: true
                })}
            </Fragment>
        )

    }
}

const mapStateToProps = state => {
    let dataSource = state.patentData.patentDataTableList
    let selectedIds = state.patentData.selectedIds
    let selectedRows = state.patentData.selectedRows
    let patentDataTableLoading = state.loading.effects['patentData/getPatentDataTableList']
    let total = state.patentData.total
    let pageSize = state.patentData.pageSize
    let current = state.patentData.current
    let queryParam = state.patentData.queryParam
    return {
        dataSource,
        selectedIds,
        selectedRows,
        patentDataTableLoading,
        total,
        pageSize,
        current,
        queryParam
    }
}

export default connect(mapStateToProps)(PatentDataTable)